import 'package:air_audio/global.dart';
import 'package:air_audio/iconfont.dart';
import 'package:air_audio/manager.dart';
import 'package:air_audio/sleep.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class PlayerPage extends StatefulWidget {
  PlayerPage({Key key}) : super(key: key);
  @override
  _PlayerPageState createState() => _PlayerPageState();
}

class _PlayerPageState extends State<PlayerPage> {
  @override
  void initState() {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    MainPlayerState playerState = Provider.of<MainPlayerState>(context);
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          color: Global.backgroundColor,
          image: DecorationImage(
            image: AssetImage('assets/images/${playerState.playing['cover']}'),
            repeat: ImageRepeat.repeat,
            fit: BoxFit.cover,
          ),
        ),
        child: Stack(
          children: [
            Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              decoration: BoxDecoration(
                color: Global.backgroundColor.withOpacity(.6),
              ),
            ),
            Positioned(
              top: 0,
              left: 0,
              child: Container(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height,
                padding: EdgeInsets.only(top: Global.padding.top),
                decoration: BoxDecoration(),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SleepPage(),
                    SizedBox(height: 30),
                    Text(
                      playerState.playing['name'],
                      style: TextStyle(
                        color: Global.fontColor.withOpacity(.8),
                        fontSize: 33,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    SizedBox(height: 30),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        InkWell(
                          onTap: () {
                            var _index = playerState.audioIndex - 1 < 0
                                ? Global.audios.length - 1
                                : playerState.audioIndex - 1;
                            playerState.setPlaying(
                                Global.audios[_index], _index);
                          },
                          child: Icon(
                            IconFont.music83,
                            color: Global.fontColor.withOpacity(.7),
                            size: 50,
                          ),
                        ),
                        SizedBox(width: 20),
                        InkWell(
                          onTap: () {
                            playerState.complete
                                ? playerState.pause()
                                : playerState.play();
                          },
                          child: Icon(
                            playerState.complete
                                ? Icons.pause_circle_filled
                                : Icons.play_circle_fill,
                            color: Global.fontColor.withOpacity(.7),
                            size: 100,
                          ),
                        ),
                        SizedBox(width: 20),
                        InkWell(
                          onTap: () {
                            var _index = playerState.audioIndex + 1 >=
                                    Global.audios.length
                                ? 0
                                : playerState.audioIndex + 1;
                            playerState.setPlaying(
                                Global.audios[_index], _index);
                          },
                          child: Icon(
                            IconFont.music82,
                            color: Global.fontColor.withOpacity(.7),
                            size: 50,
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),
            ),
            Positioned(
              top: Global.padding.top,
              left: 20,
              child: InkWell(
                onTap: () {
                  Navigator.of(context).pop();
                },
                child: Icon(
                  Icons.arrow_back,
                  color: Global.fontColor,
                  size: 34,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
